package com.example;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.time.LocalDate;

public class SemesterDialogController {
    @FXML private Label titleLabel;
    @FXML private TextField nameField;
    @FXML private DatePicker startDatePicker;
    @FXML private DatePicker endDatePicker;
    @FXML private ComboBox<String> statusComboBox;

    private Stage dialogStage;
    private Semester semester;
    private boolean confirmed = false;
    private SemesterDAO semesterDAO;

    @FXML
    public void initialize() {
        semesterDAO = new SemesterDAO();

        // 初始化状态列表
        ObservableList<String> statuses = FXCollections.observableArrayList(
            "未开始",
            "进行中",
            "已结束"
        );
        statusComboBox.setItems(statuses);
    }

    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
    }

    public void setSemester(Semester semester) {
        this.semester = semester;

        if (semester != null) {
            // 编辑模式
            titleLabel.setText("编辑学期");
            nameField.setText(semester.getName());
            startDatePicker.setValue(semester.getStartDate());
            endDatePicker.setValue(semester.getEndDate());
            statusComboBox.setValue(semester.getStatus());
        } else {
            // 添加模式
            titleLabel.setText("添加学期");
            nameField.clear();
            startDatePicker.setValue(LocalDate.now());
            endDatePicker.setValue(LocalDate.now().plusMonths(4));
            statusComboBox.getSelectionModel().selectFirst();
        }
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    @FXML
    private void handleSave() {
        if (isInputValid()) {
            String name = nameField.getText().trim();
            LocalDate startDate = startDatePicker.getValue();
            LocalDate endDate = endDatePicker.getValue();
            String status = statusComboBox.getValue();

            if (semester == null) {
                // 添加新学期
                semester = new Semester(0, name, startDate, endDate, status);
                if (semesterDAO.addSemester(semester)) {
                    confirmed = true;
                    dialogStage.close();
                } else {
                    showAlert(Alert.AlertType.ERROR, "错误", "添加学期失败");
                }
            } else {
                // 更新现有学期
                semester.setName(name);
                semester.setStartDate(startDate);
                semester.setEndDate(endDate);
                semester.setStatus(status);

                if (semesterDAO.updateSemester(semester)) {
                    confirmed = true;
                    dialogStage.close();
                } else {
                    showAlert(Alert.AlertType.ERROR, "错误", "更新学期失败");
                }
            }
        }
    }

    @FXML
    private void handleCancel() {
        dialogStage.close();
    }

    private boolean isInputValid() {
        String errorMessage = "";

        if (nameField.getText() == null || nameField.getText().trim().isEmpty()) {
            errorMessage += "学期名称不能为空\n";
        }

        if (startDatePicker.getValue() == null) {
            errorMessage += "请选择开学时间\n";
        }

        if (endDatePicker.getValue() == null) {
            errorMessage += "请选择结束时间\n";
        } else if (startDatePicker.getValue() != null && 
                  endDatePicker.getValue().isBefore(startDatePicker.getValue())) {
            errorMessage += "结束时间不能早于开学时间\n";
        }

        if (statusComboBox.getValue() == null) {
            errorMessage += "请选择学期状态\n";
        }

        if (errorMessage.isEmpty()) {
            return true;
        } else {
            showAlert(Alert.AlertType.ERROR, "输入错误", errorMessage);
            return false;
        }
    }

    private void showAlert(Alert.AlertType type, String title, String content) {
        Alert alert = new Alert(type);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(content);
        alert.showAndWait();
    }
} 